<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery1">
        <a-row :gutter="24">
        <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="盘点门店">
              <a-cascader
                :options="departModel.departTreeOptions"
                :showSearch="true"
                expand-trigger="hover"
                placeholder=""
                :fieldNames="departModel.fieldNames"
                style="width: 210px"
                v-model="queryParam.selecteddeparts"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="7" :md="8" :sm="24">
            <a-form-item label="盘点时间">
              <a-range-picker
                :ranges="{ 今天: [moment(), moment()], 近7天:[moment().subtract(7, 'days'),moment()],本月: [moment().startOf('month'), moment().endOf('month')] }"
                v-model="orderDateRange"
                @change="handleChange"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="单号">
              <a-input v-model="queryParam.billNo" allowClear></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery1" icon="search">查询</a-button>
              <a-button type="default" @click="searchReset1" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
  

    <!-- 操作按钮区域 -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <!-- ：new新建 approve申请 audit_pass通过 audit_refuse不通过 invalid作废 -->
        <span slot="bizStatus" slot-scope="text">
          <a-tag color="orange" v-if="text == 'approve'">待审核</a-tag>
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">审核</a>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <posStockCheck-modal ref="modalForm" @ok="modalFormOk"></posStockCheck-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import PosStockCheckModal from './modules/PosStockCheckModal'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { queryDepartTreeList1 } from '@/api/api'
import moment from 'moment'

export default {
  name: 'PosStockCheckList',
  mixins: [JeecgListMixin],
  components: {
    PosStockCheckModal,
  },
  data() {
    return {
      description: '盘点审核管理页面',
      disableMixinCreated: true,
      departModel: {
        departTreeOptions: [],
        fieldNames: { label: 'title', value: 'value', children: 'children' },
      },
      orderDateRange: [],
      moment,
      // 表头
      columns: [
        {
          title: '所属机构',
          align: 'center',
          dataIndex: 'orgName',
        },
        {
          title: '门店',
          align: 'center',
          dataIndex: 'shopName',
        },
        {
          title: '单号',
          align: 'center',
          dataIndex: 'billNo',
        },
        {
          title: '盘点类型',
          align: 'center',
          dataIndex: 'type',
          customRender: (text,record) => {
            if(text == 'all'){
              return '全盘'
            }
            if(text == 'brand'){
              return '按品牌['+ record.brandName +']'
            }
          },
        },
        {
          title: '经办人',
          align: 'center',
          dataIndex: 'bizUserName',
        },
        {
          title: '盘点时间',
          align: 'center',
          dataIndex: 'bizDate',
        },
        {
          title: '审核状态',
          align: 'center',
          dataIndex: 'bizStatus',
          scopedSlots: { customRender: 'bizStatus' },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/stock/check/posStockCheck/list',
      },
    }
  },
  computed: {},
  created() {
    this.init()
  },
  methods: {
    init() {

      this.queryParam = {}
      this.queryParam.bizStatus = "'approve'"
      this.orderDateRange = []
      this.loadData(1)
      this.loadDepartTree()
    },
    searchReset1() {
      this.init()
    },
    assembleQueryParam() {
      console.log(this.queryParam.selecteddeparts)
      let selecteddeparts = this.queryParam.selecteddeparts
      if (selecteddeparts && selecteddeparts.length == 2) {
        this.queryParam.orgIds = selecteddeparts[0]
        this.queryParam.shopIds = selecteddeparts[1] == 0 ? null : selecteddeparts[1]
      } else {
        this.queryParam.orgIds = null
        this.queryParam.shopIds = null
      }

    },
    searchQuery1() {
      this.assembleQueryParam()

      this.loadData(1)
    },
    handleChange(date, dateString) {
      this.queryParam.beginTime = dateString[0]
      this.queryParam.endTime = dateString[1]
    },
    loadDepartTree() {
      queryDepartTreeList1().then((res) => {
        if (res.success) {
          let departList = res.result
          if (departList.length == 1 && departList[0].orgType == 1) {
            departList = departList[0].children
          }
          this.departModel.departTreeOptions = departList

          this.departModel.departTreeOptions.forEach((x) => {
            if (!x['children']) {
              x['children'] = []
            }
            let allShop = { title: '全部门店', value: '0' }
            x['children'].unshift(allShop)
          })
        }
      })
    },
    handleDetail(record) {
      this.$refs.modalForm.view(record)
      this.$refs.modalForm.title = '盘点单详情'
      this.$refs.modalForm.disableSubmit = false
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>